package com.example.restaurantclient.Adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.anshi.shopchangepro.selfview.EaseImageView;
import com.example.restaurantclient.Entities.Dish;
import com.example.restaurantclient.Entities.ShopCart;
import com.example.restaurantclient.Entities.Variable;
import com.example.restaurantclient.R;

import java.util.ArrayList;
import java.util.List;

public class ShopcartAdapter extends BaseAdapter {
    private Context mContext;
    private List<ShopCart> shopCarts=new ArrayList<>();
    private int itemLayoutRes;
    private Handler handler;


    public ShopcartAdapter(Context mContext, List<ShopCart> shopCarts, int itemLayoutRes, Handler handler) {
        this.mContext = mContext;
        this.shopCarts = shopCarts;
        this.itemLayoutRes = itemLayoutRes;
        this.handler=handler;
    }

    @Override
    public int getCount() {
        if (shopCarts!=null){
            return shopCarts.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (shopCarts!=null){
            return shopCarts.get(position);
        }
        return null;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView==null){
            convertView= LayoutInflater.from(mContext).inflate(itemLayoutRes,null);
            holder=new ViewHolder();
            holder.dishName=convertView.findViewById(R.id.goods_name);
            holder.dishDec=convertView.findViewById(R.id.goods_desc);
            holder.dishNum=convertView.findViewById(R.id.goods_num_tv);
            holder.dishPrice=convertView.findViewById(R.id.tv_dish_price);
            holder.dishImg=convertView.findViewById(R.id.goods_img);
            holder.btnAdd=convertView.findViewById(R.id.goods_add_iv);
            holder.btnDecrease=convertView.findViewById(R.id.goods_decrease_iv);
            holder.checkBox=convertView.findViewById(R.id.cb_check);
            convertView.setTag(holder);
        }else{
            holder=(ViewHolder) convertView.getTag();
        }
        //设置控件内容
        Log.e("数据源的大小",shopCarts.size()+"");
        ShopCart shopCart=shopCarts.get(position);
        Dish dish= Variable.dishes.get(shopCart.getDishId());
        holder.dishName.setText(dish.getDishName());
        holder.dishDec.setText(dish.getMainComment());
        holder.dishPrice.setText(dish.getDishPrice()+"");
        holder.dishNum.setText(shopCart.getNumber()+"");
        holder.dishImg.setImageResource(mContext.getResources()
                .getIdentifier(dish.getDishImg(),"drawable",mContext.getPackageName()));

        //设置购物数量的加减
        final ViewHolder finalHolder = holder;
        holder.btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int sum=Integer.parseInt(finalHolder.dishNum.getText().toString());
                int sumadd=sum+1;
                finalHolder.dishNum.setText(""+sumadd);
            }
        });
        holder.btnDecrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int sum=Integer.parseInt(finalHolder.dishNum.getText().toString());
                int sumless=sum-1;
                if(sum>1){
                    finalHolder.dishNum.setText(""+sumless);
                }
            }
        });
        //设置复选框
        CheckBox checkBox=convertView.findViewById(R.id.cb_check);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               Message message=new Message();
               double unitprice=Double.parseDouble(finalHolder.dishPrice.getText().toString());
               int num=Integer.parseInt(finalHolder.dishNum.getText().toString());
               double total=num*unitprice;
               if (isChecked){
                   message.what=position;
                   message.obj=total;
                   message.arg1=num;
                   Log.e("adapter 传递的数据",total+"");
                   handler.sendMessage(message);
               }else{
                  message.what=position;
                  message.obj=total*(-1);
                  message.arg1=num;
                   Log.e("adapter 传递的数据",message.obj.toString());
                  handler.sendMessage(message);
               }
           }
       });
        return convertView;
    }
    class ViewHolder{
        TextView dishName;
        TextView dishPrice;
        TextView dishDec;
        TextView dishNum;
        EaseImageView dishImg;
        Button btnAdd;
        Button btnDecrease;
        CheckBox checkBox;
    }
}
